package com.yanqu.road.dao.impl.player;

import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.impl.TempDao;
import com.yanqu.road.entity.message.BanChatUserData;
import com.yanqu.road.utils.string.StringUtils;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ChatControlDaoImpl extends TempDao {

    public List<BanChatUserData> getBanChatUserDataMap(int channelId) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "select * from t_s_user_chat_control where channelId=?";
        List<BanChatUserData> list = new ArrayList<>();
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            call.setInt(1, channelId);
            rs = call.executeQuery();
            while (rs.next()) {
                BanChatUserData data = initUserChatData(rs);
                list.add(data);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return list;
    }

    public boolean addBanChatUserData(BanChatUserData data) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "insert into t_s_user_chat_control (userId,messageType,serverId,channelId,hiddenTime,silenceEndTime) values (?,?,?,?,?,?)";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMessageType());
            call.setLong(i++, data.getServerId());
            call.setInt(i++, data.getChannelId());
            call.setString(i++, data.getHiddenTimeParam());
            call.setLong(i++, data.getSilenceEndTime());
            if (call.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return false;
    }

    public boolean updateBanChatUserData(BanChatUserData data) {
        ResultSet rs = null;
        CallableStatement call = null;
        String sql = "update t_s_user_chat_control set hiddenTime=?,silenceEndTime=? where userId=? and messageType=? and serverId=? and channelId=?";
        try {
            getDb().setConnName(DbNameStrategy.getServerDb());
            call = getDb().getConnection().prepareCall(sql);
            int i = 1;
            call.setString(i++, data.getHiddenTimeParam());
            call.setLong(i++, data.getSilenceEndTime());
            call.setLong(i++, data.getUserId());
            call.setInt(i++, data.getMessageType());
            call.setLong(i++, data.getServerId());
            call.setInt(i++, data.getChannelId());
            if (call.executeUpdate() > 0) {
                return true;
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }

        return false;
    }

    private BanChatUserData initUserChatData(ResultSet rs) throws SQLException {
        long userId = rs.getLong("userId");
        int messageType = rs.getInt("messageType");
        BanChatUserData data = new BanChatUserData(userId, messageType);
        data.setServerId(rs.getLong("serverId"));
        data.setChannelId(rs.getInt("channelId"));
        String hiddenTime = rs.getString("hiddenTime");
        if (!StringUtils.isNullOrEmpty(hiddenTime)){
            data.setHiddenTimeParam(hiddenTime);
        }
        data.setSilenceEndTime(rs.getLong("silenceEndTime"));
        return data;
    }


}
